# Init
from Maix import GPIO,FPIOA,I2S
from fpioa_manager import fm
import nes,lcd 

def init():
    # AUDIO_PA_EN_PIN = None    # Bit Dock and old MaixGo
    # AUDIO_PA_EN_PIN = 32      # Maix Go(version 2.20)
    #AUDIO_PA_EN_PIN = 2        # Maixduino
 
    fm.register(2, fm.fpioa.GPIO1, force=True)
    wifi_en = GPIO(GPIO.GPIO1, GPIO.OUT)
    wifi_en.value(1)
    i2s = I2S(I2S.DEVICE_0)
    i2s.channel_config(i2s.CHANNEL_1,I2S.TRANSMITTER,resolution=I2S.RESOLUTION_16_BIT,cycles=I2S.SCLK_CYCLES_32,align_mode=I2S.RIGHT_JUSTIFYING_MODE)
    fm.register(34, fm.fpioa.I2S0_OUT_D1, force=True)
    fm.register(35, fm.fpioa.I2S0_SCLK, force=True)
    fm.register(33, fm.fpioa.I2S0_WS, force=True)
    lcd.init(freq=15000000)
    lcd.register(0x36,0x68)

# PS2
def ps2(cs_pin,clk_pin,mosi_pin,miso_pin,volume):
    fm.register(cs_pin, fm.fpioa.GPIOHS0)
    fm.register(clk_pin, fm.fpioa.GPIOHS1)
    fm.register(mosi_pin, fm.fpioa.GPIOHS2)
    fm.register(miso_pin, fm.fpioa.GPIOHS3)
    nes.init(nes.JOYSTICK, cs=fm.fpioa.GPIOHS0, clk=fm.fpioa.GPIOHS1, mosi=fm.fpioa.GPIOHS2, miso=fm.fpioa.GPIOHS3,vol=volume)

# keyboard
def kb(volume):	
	nes.init(nes.KEYBOARD,vol=volume)



# RUN
def run(path):
    try:
        nes.load(path)
        while True:
            nes.loop()
    except:
        nes.free()